/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.djinfo.work.activiti.controller;


import com.djinfo.work.activiti.service.ActModelService;
import com.djinfo.work.common.bean.AjaxJson;
import com.djinfo.work.dao.DictionariesDao;
import com.djinfo.work.entity.Dictionaries;
import com.djinfo.work.util.PageSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.List;

import static com.djinfo.work.common.aspect.ExceptionLogAdvice.logger;

/**
 * 流程模型相关Controller
 * @author ThinkGem
 * @version 2013-11-03
 */
@Controller
@RequestMapping("/actmodel")
public class ActModelController{

	@Autowired
	private ActModelService actModelService;

	@Autowired
	private DictionariesDao dictionariesDao;

	/**
	 * 流程模型列表
	 */
	@RequestMapping(value = "list.do")
	@ResponseBody
	public AjaxJson modelList(String category, @RequestParam("current") int current, @RequestParam("pagesize") int pagesize) {
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson = actModelService.modelList(current, pagesize, category);
		return ajaxJson;
	}

	/**
	 * 流程分类
	 */
	@RequestMapping(value = "processType.do")
	@ResponseBody
	public AjaxJson processType() {
		AjaxJson ajaxJson = new AjaxJson();
		HashMap<String, Object> map = new HashMap<>();
		String type="category";
		List<Dictionaries> categoryList =dictionariesDao.processTypeList(type);
		map.put("categoryList",categoryList);
		ajaxJson.setResult(map);
		return ajaxJson;
	}

	/**
	 * 创建模型
	 */
	@RequestMapping(value = "create1.do", method = RequestMethod.POST)
	public String create(Model model) {
		return "activiti/act/actModelCreate.jsp";
	}
	
	/**
	 * 创建模型
	 */
	@RequestMapping(value = "create2.do", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson create(String name, String key, String description, String category,
					   HttpServletRequest request, HttpServletResponse response) {
		AjaxJson ajaxJson = new AjaxJson();
		HashMap<String,Object> map = new HashMap<String,Object>();
		try {
			org.activiti.engine.repository.Model modelData = actModelService.create(name, key, description, category);
			map.put("modelId",modelData.getId());
//			response.sendRedirect(request.getContextPath() + "/page/activiti/act/act/process-editor/modeler.jsp?modelId=" + modelData.getId());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("创建模型失败：", e);
			ajaxJson.setCode(201);
			ajaxJson.setMsg("创建模型失败");
		}
		ajaxJson.setResult(map);
		return ajaxJson;
	}

	/**
	 * 根据Model部署流程
	 */
	@RequestMapping(value = "deploy.do")
	@ResponseBody
	public AjaxJson deploy(String id, RedirectAttributes redirectAttributes) {
		AjaxJson ajaxJson = new AjaxJson();
		String message = actModelService.deploy(id);
//		redirectAttributes.addFlashAttribute("message", message);
		HashMap<String, Object> map = new HashMap<>();
		map.put("message",message);
		ajaxJson.setResult(map);
		return ajaxJson;
	}
	
	/**
	 * 导出model的xml文件
	 */
	@RequestMapping(value = "export.do")
	public void export(String id, HttpServletResponse response) {
		actModelService.export(id, response);
	}

	/**
	 * 更新Model分类
	 */
	@RequestMapping(value = "updateCategory.do")
	@ResponseBody
	public AjaxJson updateCategory(String id, String category, RedirectAttributes redirectAttributes) {
		actModelService.updateCategory(id, category);
		AjaxJson ajaxJson = new AjaxJson();
		HashMap<String, Object> map = new HashMap<>();
		map.put("message","设置成功，模块ID=" + id);
		ajaxJson.setResult(map);
		return ajaxJson;
	}
	
	/**
	 * 删除Model
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "delete.do")
	@ResponseBody
	public AjaxJson delete(String id, RedirectAttributes redirectAttributes) {
		actModelService.delete(id);
		AjaxJson ajaxJson = new AjaxJson();
		HashMap<String, Object> map = new HashMap<>();
		map.put("message","删除成功，模型ID=" + id);
		ajaxJson.setResult(map);
		return ajaxJson;
	}

	@RequestMapping(value = "addmodel.do")
	public String addmodel(){
		return "activiti/model/add.html";
	}
}
